import AMap from 'AMap'
export default {
  data() {
    return {
      driving: null,
      time: '',
      num: ''
    }
  },
  methods: {
    /**
     * 初始化驾车计算模块
     */
    initDriving() {
      AMap.plugin('AMap.Driving', () => {
        this.driving = new AMap.Driving({
          policy: AMap.DrivingPolicy.REAL_TRAFFIC,
          ferry: 1
        })
      })
    },
    /**
     * 计算驾车时间
     */
    getTime(position, center) {
      if (position && center) {
        this.driving.search(position, center, (status, result) => {
          if (status === 'complete') {
            let time = parseInt(result.routes[0].time / 60)
            if (time < 60) {
              this.time = '驾车约' + time + '分钟'
            } else {
              this.time = `驾车约${parseInt(time / 60)}小时${time % 60}分钟`
            }
          } else {
            this.time = '无法预计驾车时间'
          }
        })
      }
    },
    /**
     * 计算实际距离
     */
    distance(position, center) {
      if (position && center) {
        let num = 0
        num = parseInt(AMap.GeometryUtil.distance(position, center))
        this.returnNum(num)
      }
    },
    returnNum(num) {
      if (num > 1000) {
        this.num = `距您${Math.floor(num / 100) / 10}km`
      } else {
        this.num = `距您${num}m`
      }
    }
  }
}
